package com.harvey.coding.lambda.cart;

import java.util.ArrayList;
import java.util.List;

public class CartService {
    private static List<Sku> cartSkuList = new ListGenerator<Sku>().generateBySteam(
            new Sku(20210001, "无人机", 4599.00, 10, 45990.00, SkuCategoryEnum.ELECTRONICS),
            new Sku(20210002, "VR一体机", 2500.00, 3, 7500.00, SkuCategoryEnum.ELECTRONICS),
            new Sku(20210003, "纯色衬衫", 249.00, 5, 1245.00, SkuCategoryEnum.CLOTHING),
            new Sku(20210004, "牛仔裤", 149.00, 2, 298.00, SkuCategoryEnum.CLOTHING),
            new Sku(20210005, "跑步机", 5500.00, 7, 38500.00, SkuCategoryEnum.SPORTS),
            new Sku(20210006, "Java编程思想", 75.00, 2, 150.00, SkuCategoryEnum.BOOKS),
            new Sku(20210007, "算法设计", 60.00, 8, 400.00, SkuCategoryEnum.BOOKS),
            new Sku(20210008, "Spring核心", 55.00, 4, 220.00, SkuCategoryEnum.BOOKS),
            new Sku(20210009, "系统设计师", 35.25, 9, 317.25, SkuCategoryEnum.BOOKS));
    public static List<Sku> getCartSkuList() {
        return cartSkuList;
    }

    /**
     * 从商品列表中过滤出电子类产品
     * @param cartSkuList 商品列表
     * @return 电子类的产品
     */
    public static List<Sku> filterElectronicsSkus(List<Sku> cartSkuList) {
        List<Sku> result = new ArrayList<>();
        for (Sku sku : cartSkuList) {
            if (SkuCategoryEnum.ELECTRONICS == sku.getSkuCategory()) {
                result.add(sku);
            }
        }
        return result;
    }

    /**
     * 从商品列表中过滤出指定类型的产品
     * @param cartSkuList 商品列表
     * @param skuCategoryEnum 指定类型
     * @return 电子类的产品
     */
    public static List<Sku> filterElectronicsSkusByCategory(List<Sku> cartSkuList, SkuCategoryEnum skuCategoryEnum) {
        List<Sku> result = new ArrayList<>();
        for (Sku sku : cartSkuList) {
            if (skuCategoryEnum == sku.getSkuCategory()) {
                result.add(sku);
            }
        }
        return result;
    }

    /**
     * 通过使用SkuPredicate谓词来判断，支持灵活的定制
     * @param cartSkuList 带过滤的列表
     * @param skuPredicate 谓词
     * @return 过滤后的列表
     */
    public static List<Sku> filterSkuListByPredicate(List<Sku> cartSkuList, SkuPredicate skuPredicate){
        List<Sku> result = new ArrayList<>();
        for (Sku sku : cartSkuList) {
            if(skuPredicate.test(sku)){
                result.add(sku);
            }
        }
        return result;
    }

    /**
     * 在定义了谓词接口的基础上，通过定义匿名内部类的方式
     * @param cartSkuList
     * @return
     */
    public static List<Sku> filterSkuListByAnonymousPredicate(List<Sku> cartSkuList){
        return CartService.filterSkuListByPredicate(cartSkuList, new SkuPredicate() {
            @Override
            public boolean test(Sku sku) {
                if (SkuCategoryEnum.ELECTRONICS == sku.getSkuCategory()) {
                    return true;
                }else {
                    return false;
                }
            }
        });
    }

    /**
     * 在filterSkuListByPredicate的基础上，通过lambda把匿名内部类也省去
     * @param cartSkuList
     * @return
     */
    public static List<Sku> filterSkuListByLambdaPredicate(List<Sku> cartSkuList) {
        return CartService.filterSkuListByPredicate(cartSkuList, s -> SkuCategoryEnum.ELECTRONICS == s.getSkuCategory());
    }

}
